import random


class ScoreSetNode:
    def __init__(self):
        self._last_index = 0
        self._members = list()

    def members_count(self):
        return len(self._members)

    def members(self):
        return self._members

    def add(self, member):
        self._members.append(member)

    def extends(self, members):
        self._members.extend(members)

    def loop_get(self):
        # if self._last_index + 1 > len(self._members):
        #     self._last_index = (self._last_index + 1) % len(self._members)
        # ret = self._members[self._last_index]
        # self._last_index += 1
        return self._members[self._last_index]

    def __str__(self):
        return ';'.join(self._members)


class ScoreSet:
    def __init__(self):
        self._score_dict = dict()
        self._current_score = 0

    def show_score_set(self):
        for k, v in self._score_dict.items():
            print("key:{}, value:{}".format(k, v))
        print("show_score_set" + '-' * 50)

    def add(self, in_str):
        in_list = in_str.strip().strip('.').split('.')
        score_list = [item for item in in_list if item.isdigit()]
        name_list = [item for item in in_list if not item.isdigit()]

        the_score = self.__calculate_score(score_list)
        if the_score is None:
            return False
        the_member = '.'.join(name_list)

        if str(the_score) not in self._score_dict.keys():
            ssn = ScoreSetNode()
            ssn.add(the_member)
            self._score_dict[str(the_score)] = ssn
        else:
            self._score_dict[str(the_score)].add(the_member)

        return True

    def random_get(self):
        rr = random.randint(1, self._current_score)
        temp_sum = 0
        for k, v in sorted(self._score_dict.items(), key=lambda x:int(x[0])):
            temp_sum += int(k)
            if rr <= temp_sum:
                return v.loop_get()

    def __calculate_score(self, in_list):
        float_ret = 1
        for item in in_list:
            float_ret *= float(item) / 100.0

        int_ret = int(round(float_ret, 2) * 100)
        if self._current_score + int_ret > 100:
            return None
        else:
            self._current_score += int_ret
            return int_ret


